package com.probiz.estoremf.catalog.web.action;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.Constants;
import com.probiz.estore.catalog.service.ContentCategoryManager;
import com.probiz.estore.common.model.catalog.ContentCategory;
import com.probiz.estore.common.model.content.Content;
import com.probiz.estore.content.model.ContentSearchCriteria;
import com.probiz.estore.content.service.ContentManager;
import com.probiz.estore.core.controller.GenericStoreFrontController;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.core.search.SearchCriteria;


public class ContentCategoryFrontController extends	GenericStoreFrontController<ContentCategory> {

	private ContentManager contentManager = null;
	private ContentCategoryManager contentCategoryManager = null;

	protected void initController() throws Exception {
		mgr = contentCategoryManager;
	}
	
	public void setContentManager(ContentManager contentManager) {
		this.contentManager = contentManager;
	}

	public void setContentCategoryManager(
			ContentCategoryManager contentCategoryManager) {
		this.contentCategoryManager = contentCategoryManager;
	}


	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {
		// -----start:获得内容目录------
		Integer categoryId = contentCategoryManager.getCategoryIdByUri(request.getRequestURI());
		ContentCategory contentCategory = contentCategoryManager.getById(categoryId);
		// 没有找到相应目录不能访问,直接进入找不到或已删除页面
		if (contentCategory == null || !contentCategory.getStatus().equals(Constants.FLAG_TRUE)) {
			try {
				response.sendError(404);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		// -----end:获得内容目录------
		

		SearchCriteria searchCriteria = createSearchCriteria(request, BaseObject.FilterHqlName.DEFAULT);
		// ------start:构造前台查询条件------
		ContentSearchCriteria contentSearchCriteria = new ContentSearchCriteria();
		
		bindOnly(request, contentSearchCriteria, "contentSearchCriteria");
		contentSearchCriteria.setStatus(Content.STATUS_PUBLISHED);
		contentSearchCriteria.setCategoryId(contentCategory.getCategoryId().toString());
		contentSearchCriteria.setOrderby((short)1);
		// ------end:构造前台查询条件------
		List<Content> contentList = contentManager.searchByCriteriaForController(contentSearchCriteria,	searchCriteria);

		ModelAndView mv = new ModelAndView("content/defaultContentList");
		mv.addObject("contentList", contentList);
		mv.addObject("category", contentCategory);
		mv.addObject("contentSearchCriteria", contentSearchCriteria);
		
		// ------start:把页面字体大小设置到attribute中------
		Cookie[] cookies = request.getCookies();
		if (!ArrayUtils.isEmpty(cookies)) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("listFontSize")) {
					mv.addObject("listFontSize", cookie.getValue());
				}
			}
		}
		// ------end:把页面字体大小设置到attribute中------

		return mv;

	}
	
	public ModelAndView loadMore(HttpServletRequest request,
			HttpServletResponse response) {
		// -----start:获得内容目录------
		Integer categoryId = contentCategoryManager.getCategoryIdByUri(request.getRequestURI());
		ContentCategory contentCategory = contentCategoryManager.getById(categoryId);
		// 没有找到相应目录不能访问,直接进入找不到或已删除页面
		if (contentCategory == null || !contentCategory.getStatus().equals(Constants.FLAG_TRUE)) {
			try {
				response.sendError(404);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		// -----end:获得内容目录------
		

		SearchCriteria searchCriteria = createSearchCriteria(request, BaseObject.FilterHqlName.DEFAULT);
		
		searchCriteria.getPageNo();
		//searchCriteria.changePaging(searchCriteria.getPageNo()+1, defaultPageSize);
		searchCriteria.getTotalPageCount();
		
		
		// ------start:构造前台查询条件------
		ContentSearchCriteria contentSearchCriteria = new ContentSearchCriteria();
		
		bindOnly(request, contentSearchCriteria, "contentSearchCriteria");
		contentSearchCriteria.setStatus(Content.STATUS_PUBLISHED);
		contentSearchCriteria.setCategoryId(contentCategory.getCategoryId().toString());
		contentSearchCriteria.setOrderby((short)1);
		// ------end:构造前台查询条件------
		List<Content> contentList = contentManager.searchByCriteriaForController(contentSearchCriteria,	searchCriteria);

		ModelAndView mv = new ModelAndView("content/include/contentListBody");
		mv.addObject("contentList", contentList);

		return mv;

	}

}